package com.qingxin.cms.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.cms.entity.CmsArticle;
import com.qingxin.cms.entity.CmsArticleAttachment;
import com.qingxin.cms.service.CmsArticleAttachmentService;
import com.qingxin.cms.service.CmsArticleService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.MyUploadFile;
import com.qingxin.sys.entity.MyUploadFileParam;
import com.qingxin.sys.entity.MyUploadFiles;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;

/**
 * 文章管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("cms/article")
public class CmsArticleAction extends BaseAction {
	
	@Autowired
	private CmsArticleService cmsArticleService;
	@Autowired
	private CmsArticleAttachmentService cmsArticleAttachmentService;
	
	
	/**
	 * 文章列表数据
	 * @param pageParam
	 * @param search
	 * @param leftdata
	 * @param articleTitle
	 * @param keyWord
	 * @param articleDesc
	 * @param articleContent
	 * @param createBy
	 * @param createTime
	 * @param updateBy
	 * @param updateTime
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(PageParam pageParam,String search,String leftdata,HttpServletRequest request){
		return cmsArticleService.list(pageParam, search, leftdata, request);
	}
	
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/cms/article/article_list";
	}
	
	/**
	 * 添加文章
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, String leftdata, HttpServletRequest request){
		model.put("categoryId", leftdata);
		return "/trace/cms/article/article_add";
	}
	
	/**
	 * 保存文章信息
	 * @param article
	 * @param result
	 * @param categoryId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid CmsArticle article ,BindingResult result, String categoryId, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		if(article.getCarousel() == null){
			article.setCarousel(0);
		}
		
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);
		
		SysError info = cmsArticleService.add(article,categoryId,uploadFileParam,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 修改文章
	 * @param model
	 * @param articleId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String articleId, HttpServletRequest request){
		CmsArticle cmsArticle = cmsArticleService.get(articleId);
		model.put("article", cmsArticle);
	
		List<CmsArticleAttachment> list = cmsArticleAttachmentService.list(articleId);
		MyUploadFiles attachments = new MyUploadFiles();
		for(CmsArticleAttachment att : list){
			attachments.addFile(new MyUploadFile(att.getId(), att.getFilePath(), att.getFilePath(), att.getFileName(), null));
		}
		model.put("attachments", attachments);
		return "trace/cms/article/article_edit";
	}
	
	/**
	 * 保存修改
	 * @param article
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid CmsArticle article, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		if(article.getCarousel() == null){
			article.setCarousel(0);
		}
		MyUploadFileParam uploadFileParam = new MyUploadFileParam("attachment", request);
		return cmsArticleService.update(article, uploadFileParam, getLoginUser(request), request);
	}
	
	/**
	 * 删除文章
	 * @param articleId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] articleId, HttpServletRequest request){
		return cmsArticleService.delete(articleId, request);
	}
}
